<?php
/**
 * Created by PhpStorm.
 * User: DELL
 * Date: 2018/11/13
 * Time: 22:23
 */

namespace app\home\controller;
use app\home\model\Check as CheckModel;
use app\home\model\CheckSchool;
use app\home\model\CheckPaper;
use app\home\model\WechatUser;
use app\home\model\Write;
use think\Db;
/**
 * Class Check
 * @package app\home\controller
 * 招生简章 / 招生信息
 */
class Check extends Base
{
    /**
     * 首页
     */
    public function index(){
        $userId = session('userId');
        $User = WechatUser::where(['openid' => $userId])->field('subscribe,review_status')->find();
        if ($User['review_status'] == 0 || $User['subscribe'] == 0){
            $this->redirect('Journal/null');
        }
        $this->jssdk();
        // 考试年份
        $y = date('Y')-2;
        $year = [];
        for ($i=0;$i<4;$i++){
            array_push($year,$y+$i);
        }
        $this->assign('year',$year);
        // 学校地区
        $area = CheckModel::where([])->order('dq','asc')->field('dq,dq_id')->select();
        $this->assign('area',$area);
        // 学校
        $school = CheckSchool::where('dq_id',12)->field('dx,dx_id')->order('dx','asc')->select();
        $this->assign('school',$school);
        // 学院
        $home = [];
        $this->assign('home',$home);
        // 专业
        $zy = [];
        $this->assign('zy',$zy);
        $this->assign('nf',date('Y')+1);
        $Model = new Write();
        $Write = $Model->where(['openid' => $userId])->field('mb_mc')->find();
        $sc = 0;
        if (!empty($Write)){
            $Dx_id = CheckSchool::where('dx',$Write['mb_mc'])->value('dx_id');
            $sc = '***';
            if ($Dx_id){
                $sc = $Dx_id;
            }
        }
        $this->assign('sc',$sc);
        return $this->fetch();
    }
    /**
     * 根据地区选学校*/
    public function sele(){
        $id = input('id');
        $list = CheckSchool::where('dq_id',$id)->field('dx,dx_id')->order('dx','asc')->select();
        return $list;
    }
    /**
     * 根据学校 选学院
     */
    public function selec(){
        $id = input('id');
        $list = Db::table('pb_yxzy')->where('school_id',$id)->group('xy')->select();
        return $list;
    }
    /**
     * 根据学校 学院 选择 专业
     */
    public function select(){
        $id = input('id'); // 学校 id
        $name = input('name'); // 学院
        $list = Db::table('pb_yxzy')->where(['school_id' => $id,'xy' => $name])->order('zy','asc')->select();
        return $list;
    }
    /**
     * 数据列表页
     */
    public function info(){
        $userId = session('userId');
        $User = WechatUser::where(['openid' => $userId])->field('subscribe,review_status')->find();
        if ($User['review_status'] == 0 || $User['subscribe'] == 0){
            $this->redirect('Journal/null');
        }
        $time = input('time');  // 年份
        $school = input('school');  // 学校
        $Model = new CheckPaper();
        //招生简章
        $map1 = array(
            'nf' => $time,
            'xx' => $school,
            'fl_flg' =>1
        );
        $list1 = $Model->where($map1)->order('id desc')->field('id,title,nf,fl_flg')->select();
        $this->assign('list1',$list1);

        //招生目录
        $map2 = array(
            'nf' => $time,
            'xx' => $school,
            'fl_flg' =>2
        );
        $list2 = $Model->where($map2)->order('id desc')->field('id,title,nf,fl_flg')->select();
        $this->assign('list2',$list2);

        //参考书目
        $map3 = array(
            'nf' => $time,
            'xx' => $school,
            'fl_flg' =>3
        );
        $list3 = $Model->where($map3)->order('id desc')->field('id,title,nf,fl_flg')->select();
        $this->assign('list3',$list3);

        //复试信息
        $map4 = array(
            'nf' => $time,
            'xx' => $school,
            'fl_flg' =>6
        );
        $list4 = $Model->where($map4)->order('id desc')->field('id,title,nf,fl_flg')->select();
        $this->assign('list4',$list4);

        //调剂信息
        $map5 = array(
            'nf' => $time,
            'xx' => $school,
            'fl_flg' =>7
        );
        $list5 = $Model->where($map5)->order('id desc')->field('id,title,nf,fl_flg')->select();
        $this->assign('list5',$list5);
        $this->assign('sch',$school);
        $this->assign('t',$time);
        return $this->fetch();
    }
    /*
     *人才创业列表
     */
    public function lists(){
        $userId = session('userId');
        $User = WechatUser::where(['openid' => $userId])->field('subscribe,review_status')->find();
        if ($User['review_status'] == 0 || $User['subscribe'] == 0){
            $this->redirect('Journal/null');
        }
        $Model = new CheckPaper();
        $type = input('type');
        $sch = input('school');
        $time = input('time');
        $map = array(
            'nf' => ['neq',$time],
            'xx' => $sch,
            'fl_flg' =>$type
        );
        $list = $Model->where($map)->order('nf desc')->field('id,title,nf,fl_flg')->limit(15)->select();
        $this->assign('list',$list);
        $this->assign('type',$type);
        $this->assign('time',$time);
        $this->assign('school',$sch);
        return $this->fetch();
    }
    /*
     *人才创业详情页
     */
    public function detail(){
        $userId = session('userId');
        $User = WechatUser::where(['openid' => $userId])->field('subscribe,review_status')->find();
        if ($User['review_status'] == 0 || $User['subscribe'] == 0){
            $this->redirect('Journal/null');
        }
        $this->jssdk();
        $id = input('id');
        $Model = new CheckPaper();
        $Info = $Model->where('id',$id)->field('title,content')->find();
        $reg1 = "/href=\"(\/[^\"]+)/";//获取href中的值
        preg_match_all($reg1,$Info['content'],$arr);  // 获取所有连接的 arr
        if ($arr[1]){
            foreach ($arr[1] as $value){
                // 判断系统
                if(strpos($_SERVER['HTTP_USER_AGENT'], 'iPhone')||strpos($_SERVER['HTTP_USER_AGENT'], 'iPad')){
                    // IOS
                    // 获取 href的值 并加上 http://  绝对路径
                    $val = "http://www.popedu.net".$value;
                    $Info['content'] = str_replace($value,$val,$Info['content']);
                }else{
                    // 获取 href的值 并加上 http://  绝对路径
                    $val = "http://ow365.cn/?i=17158&fname=点对点教育_文件在线预览&furl=http://www.popedu.net".$value;
                    $Info['content'] = str_replace($value,$val,$Info['content']);
                }
            }
        }
        preg_match_all('/<img.*?src=("|\')(\/.*?)("|\').*?>/is',$Info['content'],$match);
        if ($match){
            foreach ($match[2] as $value){
                // 获取 src 的值 并加上 http://  绝对路径
                $val = "http://www.popedu.net".$value;
                $Info['content'] = str_replace($value,$val,$Info['content']);
            }
        }
        $this->assign('info',$Info);
        return $this->fetch();
    }

    /**
     * 加载更多
     */
    public function listmore() {
        $len = input('length');
        $type = input('type');
        $sch = input('school');
        $time = input('time');
        $Model = new CheckPaper();
        $map = array(
            'nf' => ['neq',$time],
            'xx' => $sch,
            'fl_flg' =>$type
        );
        $list = $Model->where($map)->order('nf desc')->field('id,title,nf,fl_flg')->limit($len,15)->select();
        if($list){
            return $this->success("加载成功",'',$list);
        }else{
            return $this->error("加载失败");
        }
    }
}